package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.ReturnListDao;
import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.entity.ReturnList;
import com.atguigu.jxc.entity.ReturnListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.ReturnListGoodsService;
import com.atguigu.jxc.vo.PCountVo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fenge
 */
@Service
public class ReturnListGoodsServiceImpl implements ReturnListGoodsService {

    @Autowired
    private ReturnListGoodsDao returnListGoodsDao;

    @Autowired
    private ReturnListDao returnListDao;

    @Override
    public void save(ReturnList returnList, String returnListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        Integer userId = user.getUserId();
        returnList.setUserId(userId);
        returnListDao.save(returnList);
        Gson gson = new Gson();
        List<ReturnListGoods> returnListGoods = gson.fromJson(returnListGoodsStr,new TypeToken<List<ReturnListGoods>>(){}.getType());
        returnListGoods.forEach(returnListGood->{
            returnListGood.setReturnListId(returnList.getReturnListId());
            returnListGoodsDao.save(returnListGood);
        });
    }

    @Override
    public Map<String, Object> list(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        Map<String, Object> map = new HashMap<>();
        List<ReturnList> returnLists = returnListDao.list(returnNumber,supplierId,state,sTime,eTime);
        map.put("rows",returnLists);
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer returnListId) {
        List<ReturnListGoods> returnListGoods = returnListGoodsDao.goodsList(returnListId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows",returnListGoods);
        return map;
    }

    @Override
    public void delete(Integer returnListId) {
        List<ReturnListGoods> returnListGoods = returnListGoodsDao.goodsList(returnListId);
        returnListGoods.forEach(returnListGood->{
            returnListGoodsDao.delete(returnListGood);
        });
        returnListDao.delete(returnListId);
    }

    @Override
    public void updateState(Integer returnListId) {
        returnListDao.updateState(returnListId);
    }

    @Override
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<PCountVo> pCountVos = returnListDao.count(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        String s = gson.toJson(pCountVos);
        return s;
    }
}
/*
[{"goodsId":24,
"goodsTypeId":16,
"goodsCode":"0015",
"goodsName":"+iPhone+X",
"goodsModel":"X",
"goodsUnit":"台",
"lastPurchasingPrice":8000,
"price":"8000",
"goodsNum":"12",
"total":96000},
{"goodsId":22,"goodsTypeId":11,"goodsCode":"0014","goodsName":"美国青豆原味+蒜香","goodsModel":"250g装","goodsUnit":"袋","lastPurchasingPrice":5,"price":"5","goodsNum":"12","total":60}]
 */